﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Diagnostics;
using System.Windows;
using PetriNetSharp.View;

namespace PetriNetSharp.Converter
{
	class PointToPlaceLocationValueConverter : IValueConverter
	{

		/// <summary>
		/// Convert a passed point to either the X or the Y a PlaceView should use in
		/// it's position (so it is centered at the point)
		/// </summary>
		/// <param name="value">The point to converter</param>
		/// <param name="targetType">Not used</param>
		/// <param name="parameter">Must be of type PointToDoubleValueConverterParams, specifies to take the X or Y</param>
		/// <param name="culture">Not used</param>
		/// <returns>Either the X or Y, depending on the parameter, needed to center
		/// a PlaceView around the passed point</returns>
		/// <preconditions>
		/// Value must be of type Point
		/// Parameter must be of type PointToDoubleValueConverterParams
		/// </preconditions>
		object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			#region preconditions
			Debug.Assert(value is Point, "PointToDoubleValueConverter value must be of type point");
			Debug.Assert(parameter is PointToDoubleValueConverterParams,
				"PointToDoubleValueConverter parameter must be of type PointToDoubleValueConverterParams");
			#endregion

			Point point = (Point)value;
			PointToDoubleValueConverterParams param = (PointToDoubleValueConverterParams)parameter;
			if (param == PointToDoubleValueConverterParams.X)
			{
				return point.X - PlaceView.PlaceWidth / 2.0;
			}
			else if (param == PointToDoubleValueConverterParams.Y)
			{
				return point.Y - PlaceView.PlaceHeight / 2.0;
			}

			throw new Exception("Inproper parameter passed to PointToPlaceLocationConverter");
		}

		/// <summary>
		/// Not Implemented
		/// </summary>
		/// <param name="value">Not Implemented</param>
		/// <param name="targetType">Not Implemented</param>
		/// <param name="parameter">Not Implemented</param>
		/// <param name="culture">Not Implemented</param>
		/// <returns>Not Implemented</returns>
		object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
